package com.fll.fangyishare.service.impl;

import com.fll.fangyicommon.entity.share.Comment;
import com.fll.fangyicommon.entity.share.Liked;
import com.fll.fangyicommon.entity.share.Share;
import com.fll.fangyishare.dao.CommentDao;
import com.fll.fangyishare.dao.CommentLikedDao;
import com.fll.fangyishare.service.CommentLikedService;
import com.fll.fangyishare.service.CommentService;
import com.fll.fangyishare.service.LikedService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author fll
 * create 2023/3/25
 */
@Service
@Slf4j
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao;

    @Autowired
    private CommentLikedService commentLikedService;

    /**
     * 判断是否已评论
     * @param userId
     * @param shareId
     * @return
     */
    @Override
    public Boolean isComment(Integer userId, Integer shareId){
        //评论记录表中查询，如果有记录咋已评论过
        ArrayList<Comment> comment = commentDao.isComment(userId, shareId);
        if (comment == null || comment.size() == 0){
            return false;
        }
        return true;
    }

    /**
     * 获取评论总数
     * @param shareId
     * @return
     */
    @Override
    public Integer getCommentCount(Integer shareId){
        return commentDao.getCommentCount(shareId);
    }

    /**
     * 添加一条评论记录
     * @param comment
     * @return
     */
    @Override
    public Integer addComment(Comment comment){
        Integer num = commentDao.addComment(comment);
        if (num != 0){
            return num;
        }
        return -1;
    }

    /**
     * 获取某条share的所有评论
     * @param shareId
     * userId: 替换当前登录用户id
     * @return
     */
    @Override
    public ArrayList<Comment> getAllComment(Integer shareId, Integer userId){
        ArrayList<Comment> allComment = commentDao.getAllComment(shareId);
        if (allComment != null || allComment.size() != 0){
            for (Comment comment : allComment){
                //赋值当前用户是否点赞
                /**
                 * 此处userId不能传comment.getUserId(),comment.getUserId()是评论的所属用户
                 */
                comment.setIsLike(commentLikedService.isLike(userId, comment.getShareId(), comment.getCommentId()));
                //赋值评论的点赞数
                comment.setCommentLike(commentLikedService.getCommentLikeCount(comment.getShareId(), comment.getCommentId()));
            }
            return allComment;
        }
        return null;
    }

}
